package com.axend.aerosense.pay.viewmodel;

import com.alibaba.android.arouter.launcher.ARouter;
import com.axend.aerosense.pay.R;
import com.axend.aerosense.pay.entity.PayPurchaseRecordBean;
import com.blankj.utilcode.util.ToastUtils;
import com.axend.aerosense.base.bean.BaseDevInfoBean;
import com.axend.aerosense.base.impl.IBaseView;
import com.axend.aerosense.base.impl.ILoadDataResult;
import com.axend.aerosense.base.storage.MmkvHelper;
import com.axend.aerosense.base.utils.LogUtils;
import com.axend.aerosense.base.viewmodel.CustomBaseViewModel;
import com.axend.aerosense.common.UrlApi;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.common.router.RouterActivityPath;
import com.axend.aerosense.common.util.JsonUtil;
import com.axend.aerosense.network.EasyHttp;
import com.axend.aerosense.network.cache.model.CacheMode;
import com.axend.aerosense.network.callback.SimpleCallBack;
import com.axend.aerosense.network.exception.ApiException;

import io.reactivex.disposables.Disposable;

public class PurchaseHistroyViewModel extends CustomBaseViewModel<PayPurchaseRecordBean> {
    private Disposable disposable;
    public PurchaseHistroyViewModel(IBaseView baseViewImpl, ILoadDataResult<PayPurchaseRecordBean> loadDataResultImpl) {
        super(baseViewImpl, loadDataResultImpl);
    }

    @Override
    public void onLoad() {
        disposable = EasyHttp.post(UrlApi.URL.GetHistoricalTransaction.getUrl())
                .headers("token", MmkvHelper.getInstance().getMmkv().decodeString(Gobal.SAVE_KEY.TOKEN))
                .cacheMode(CacheMode.NO_CACHE)
                .upJson(JsonUtil.toJson(new BaseDevInfoBean()))
                .execute(new SimpleCallBack<PayPurchaseRecordBean>() {
                    @Override
                    public void onError(ApiException e) {
                        LogUtils.e("myTAG", "onError:payPurchaseRecordBeans:" + e.toString());
                        loadDataFail(e.getMessage());
                    }

                    @Override
                    public void onSuccess(PayPurchaseRecordBean payPurchaseRecordBean) {
                        loadDataSuccess(payPurchaseRecordBean);
                    }

                    @Override
                    public void onTokenExpire() {
                        ToastUtils.showShort(com.axend.aerosense.dev.R.string.common_login_first);
                        //登录界面不做处理
                        ARouter.getInstance().build(RouterActivityPath.Login.PAGE_LOGIN_HOME).withBoolean("jumpMainPage", true).greenChannel().navigation();
                    }
                });
    }

    @Override
    protected void onCleared() {
        if (null != disposable && !disposable.isDisposed()){
            disposable.dispose();
            disposable = null;
        }
        super.onCleared();
    }
}
